package view

import (
	"GoProject/YUAN/projectl/service"
	"bufio"
	"fmt"
	"os"
	"strings"
)

type CustomerView struct {
	Choice int //接收⽤户输⼊...
	CS     service.CustomerService
}

func (cv *CustomerView) nextChoice() (b bool) {
	// 引导用户选择继续还是返回
	fmt.Print("返回上一层【回车】,继续该操作【C/c】,退出【Q/q】:")
	var backChoice string
	fmt.Scanln(&backChoice)
	if strings.ToUpper(backChoice) == "C" {
		b = true
	} else if strings.ToUpper(backChoice) == "Q" {
		os.Exit(0)
	}
	return
}

func (cv *CustomerView) AddView() {

	for true {
		// 引导用户输入学号和姓名
		fmt.Printf("\033[1;35;40m%s\033[0m\n", "---------------------------添加客户开始-----------------------------")
		// 引导用户输入
		var name string
		fmt.Print("请输入客户姓名：")
		fmt.Scan(&name)

		var gender string
		fmt.Print("请输入客户性别：")
		fmt.Scan(&gender)

		var age int8
		fmt.Print("请输入客户年龄：")
		fmt.Scan(&age)

		var email string
		fmt.Print("请输入客户邮箱：")
		fmt.Scan(&email)

		// 后端接口调用，实现添加学生的逻辑
		cv.CS.AddCustomer(name, gender, age, email)

		goOn := cv.nextChoice()
		if !goOn {
			break
		}
	}

}
func (cv *CustomerView) ListView() {

	for true {
		fmt.Printf("\033[1;32;40m%s\033[0m\n", "----------------------------------客户列表开始----------------------------")
		for _, customer := range cv.CS.ListCustomer() {
			fmt.Printf("\u001B[1;39;45m编号：%-8d 姓名：%-8s 性别：%-8s 年龄：%-8d 邮箱：%-10s \u001B[0m\n",
				customer.Cid, customer.Name, customer.Gender, customer.Age, customer.Email)
		}
		fmt.Printf("\033[1;32;40m%s\033[0m\n", "----------------------------------客户列表完成----------------------------")
		goOn := cv.nextChoice()
		if !goOn {
			break
		}
	}

}

func (cv *CustomerView) UpdateView() {

	fmt.Printf("\033[1;36;40m%s\033[0m\n", "---------------------------客户修改开始----------------------------")
	for true {
		var updateCid int
		fmt.Print("请输入更新客户编号(-1退出)：")
		fmt.Scan(&updateCid)
		if updateCid == -1 {
			return
		}
		updateIndex := cv.CS.FindById(updateCid)
		if updateIndex == -1 {
			fmt.Println("删除失败，输入的编号ID不存在")
			continue
		}

		updateCustomer := cv.CS.FindCustomer(updateCid)

		// 引导用户输入
		var name string
		fmt.Printf("请输入客户姓名(%s)：", updateCustomer.Name)
		fmt.Scanln(&name)

		var gender string
		fmt.Printf("请输入客户性别(%s)：", updateCustomer.Gender)
		fmt.Scanln(&gender)

		var age int8
		fmt.Printf("请输入客户年龄(%d)：", updateCustomer.Age)
		fmt.Scanln(&age)

		var email string
		fmt.Printf("请输入客户邮箱(%s)：", updateCustomer.Email)
		fmt.Scanln(&email)

		// 后端接口调用
		cv.CS.UpdateCustomer(updateCustomer, name, age, gender, email)

		fmt.Printf("\033[1;36;40m%s\033[0m\n", "---------------------------客户修改完成----------------------------")
		goOn := cv.nextChoice()
		if !goOn {
			break
		}
	}

}

func (cv *CustomerView) DeleteView() {

	for true {
		fmt.Printf("\033[1;31;40m%s\033[0m\n", "---------------------------删除客户开始----------------------------")
		var delCid int
		fmt.Print("请输入删除客户编号：")
		fmt.Scan(&delCid)

		delIndex := cv.CS.FindById(delCid)
		if delIndex == -1 {
			fmt.Println("删除失败，输入的编号ID不存在")
			continue
		}
		cv.CS.DeleteCustomer(delIndex)

		fmt.Printf("\033[1;31;40m%s\033[0m\n", "---------------------------删除客户完成----------------------")
		goOn := cv.nextChoice()
		if !goOn {
			break
		}
	}

}

func (cv *CustomerView) KeepView() {
	cv.CS.KeepCustomers()
	fmt.Printf("\033[1;31;40m%s\033[0m\n", "---------------------------保存完成----------------------")
}

func (cv *CustomerView) MainView() {

	for true {
		fmt.Printf("\033[1;30;42m%s\033[0m\n", `
----------------客户信息管理系统--------------
           1、添加客户
           2、查看客户
           3、更新客户
           4、删除客户
           5、保存
           6、退出
-------------------------------------------
`)

		fmt.Printf("\033[1;38;40m%s\033[0m", "请输入选择【1-5】:")
		stdin := bufio.NewReader(os.Stdin)
		fmt.Fscan(stdin, &cv.Choice)

		switch cv.Choice {
		case 1:
			cv.AddView()
		case 2:
			cv.ListView()
		case 3:
			cv.UpdateView()
		case 4:
			cv.DeleteView()
		case 5:
			cv.KeepView()
		case 6:
			os.Exit(0)
		default:
			fmt.Println("按要求输入数字，请重新输入")
		}
	}

}
